package com.hoshiicloud.stock.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.stock.entity.WarehouseType;
import com.hoshiicloud.stock.param.WarehouseTypePageParam;
import com.hoshiicloud.stock.param.WarehouseTypeUpdateParam;
import com.hoshiicloud.stock.service.IWarehouseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 仓库类型controller类
 *
 * @author : yangcheng
 * @date : 2019/5/20 14:23
 * @mail : maiboyc@163.com
 **/
@Slf4j
@RestController
@RequestMapping("/warehouseType")
@Api(value = "仓库类型管理", tags = {"仓库类型操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class WarehouseTypeController extends BaseController {

    // 仓库类型Service
    @Autowired
    private IWarehouseTypeService warehouseTypeService;

    /**
     * 根据ID查询仓库类型信息
     * @param id 仓库类型ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询仓库类型信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "仓库类型主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<WarehouseType> getById(@PathVariable("id") Long id) {
        Response<WarehouseType> rs;
        try {
            rs = Response.data(warehouseTypeService.getById(id));
        } catch (Exception e) {
            log.error("根据ID查询仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 根据ID组查询仓库类型信息
     * @param ids ID组
     * @return Response
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询仓库类型信息", consumes = MediaType.ALL_VALUE)
    public Response<List<WarehouseType>> getByIds(@RequestParam("ids") List<Long> ids) {
        Response<List<WarehouseType>> rs;
        try {
            rs = Response.data(warehouseTypeService.getByIds(ids));
        } catch (Exception e) {
            log.error("根据ID组查询仓库类型信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }


    /**
     * 分页查询仓库类型信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询仓库类型信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<WarehouseType>> list(@RequestBody WarehouseTypePageParam param) {
        Response<IPage<WarehouseType>> rs;
        try {
            WarehouseType entity = new WarehouseType();
            BeanUtils.copyProperties(param, entity);
            rs = Response.data(warehouseTypeService.List(entity));
        } catch (Exception e) {
            log.error("分页查询仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 新增仓库类型
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增仓库类型", notes = "新增仓库类型", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> save(@RequestBody @Valid WarehouseType entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(warehouseTypeService.insert(entity));
            }
        } catch (ServiceException e) {
            log.error("新增仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        }catch (Exception e) {
            log.error("新增仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 修改仓库类型
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改仓库类型", notes = "修改仓库类型", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> update(@RequestBody @Valid WarehouseTypeUpdateParam param, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                WarehouseType entity = new WarehouseType();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(warehouseTypeService.update(entity));
            }
        } catch (ServiceException e) {
            log.error("更新仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("更新仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 根据ID删除仓库类型信息
     * @param id 仓库类型ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除仓库类型信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "仓库类型主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        Response<Boolean> rs;
        try {
            rs = Response.data(warehouseTypeService.delete(id));
        } catch (ServiceException e) {
            log.error("删除仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("删除仓库类型异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

}
